package AlienArena::Server::CVar::DMFlags;

use 5.006;
use strict;
use warnings;
use lib '../../..';
use base 'AlienArena::Server::CVar';
use AlienArena::Server::CVar::DMFlags::Constants;
our $VERSION = '0.01';
our %bitnames;

sub new {
    my $class = shift;
    my $value = shift || 0;
    my $self = $class->SUPER::new('dmflags',$value,qw(SERVERINFO));
    bless $self, $class;
}

sub set_flags {
    my $self = shift;
    my @flags = @_;
    my $val = $self->value;
    my $flags = 0;
    foreach (@flags) {
	my $bit;
	eval {
	    my $const = "DF_".uc($_);
	    $bit = eval $const;
	};
	unless ($@) {
	    $flags |= $bit;
	}
    }
    return $self->value($val|$flags);
}

sub clear_flags {
    my $self = shift;
    my @flags = @_;
    my $val = $self->value;
    my $flags = 0x003FFFFF;
    foreach (@flags) {
	my $bit;
	eval {
	    my $const = "DF_".uc($_);
	    $bit = eval $const;
	};
	unless ($@) {
	    $flags ^= $bit;
	}
    }
    return $self->value($val & $flags);
}

sub flag_list {
    my $self = shift;
    my @flags;
    my $val = $self->value;
    for (my $flag = 1; $flag <= 0x00200000; $flag <<= 1) {
	if ($bitnames{$flag}) {
	    push @flags,$bitnames{$flag} if ($val & $flag);
	}
    }
    return @flags;
}

sub available_flags {
    my $self = shift;
    return sort values %bitnames;
}

=head1 NAME

AlienArena::Server::CVar::DMFlags - Representation of the dmflags cvar

=head1 SYNOPSIS

    use AlienArena::Server::CVar::DMFlags;

=head1 METHODS

=head2 set_flags, clear_flags

 $dmflags->value(0);
 $dmflags->set_flags(qw(no_health instant_items force_respawn));
 $dmflags->clear_flags(qw(infinite_ammo bot_chat skin_teams));

=head2 flag_list

=head2 available_flags

=head1 AUTHOR

Mark A. Jensen, C<< <maj -at- fortinbras -dot- us> >>

=head1 LICENSE AND COPYRIGHT

Copyright 2012 Mark A. Jensen.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

1; # End of AlienArena::Server::CVar::DMFlags
